package com.woniuxy.ticketmovie.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.woniuxy.ticketmovie.entity.ResponceResult;
import com.woniuxy.ticketmovie.entity.Ticket_type;
import com.woniuxy.ticketmovie.entity.TypeAdd;
import com.woniuxy.ticketmovie.entity.findById;
import com.woniuxy.ticketmovie.service.Ticket_typeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author movie
 * @since 2021-02-21
 */
@Validated
@RestController
@RequestMapping("/ticket_type")
public class Ticket_typeController {
    @Autowired
    private Ticket_typeService typeService;

    /**
     * 添加
     * @param type
     * @param result
     * @return
     */
    @GetMapping("add")
    public ResponceResult<Object> add(@RequestBody @Validated(value = TypeAdd.class) Ticket_type type) {

//        if(result.hasErrors()){
//
//            Map<String,String> msg=new HashMap<>();
//
//            result.getFieldErrors().forEach(e->msg.put(e.getField(),e.getDefaultMessage()));
//
//            ResponceResult<Object> responceResult=new ResponceResult<>();
//
//            responceResult.setCode(101);
//
//            responceResult.setMsg("无效参数");
//
//            responceResult.setData(msg);
//
//            return responceResult;
//        }
        //常规判断
//        if (type==null){
//
//        }
//        //常规判断
//        if (type.getName()==null||type.getName().trim().length()==0){
//
//        }

            typeService.add(type);

         return  new ResponceResult<>(200,"ok");
    }

    @GetMapping("findTypeByPage")
    public IPage<Ticket_type> findTypeByPage(Integer page, Integer size){

        Page<Ticket_type> typePage=new Page<>(page,size);
        IPage<Ticket_type> typeIPage=typeService.page(typePage);

        return typeIPage;
    }

    @PutMapping("updateType")
    public ResponceResult<Void> updateType(@RequestBody@Validated(value = findById.class) Ticket_type ticket_type){


        try {
            typeService.updateById(ticket_type);
        } catch (RuntimeException e) {
            return new ResponceResult<>(501,"修改异常");
        }

        return ResponceResult.SUCCESS;
    }

    @PostMapping("findById")
    public ResponceResult<Void> findById(@PathVariable @Min(value = 1) Integer id){

        return new ResponceResult();
    }

    @DeleteMapping("deleteType")
    public ResponceResult<Void> deleteType(Integer id){

        try {
            typeService.removeById(id);
        } catch (RuntimeException e) {
            return new ResponceResult<>(502,"删除异常");
        }
        return ResponceResult.SUCCESS;
    }
}

